package kr.hkit.password.fragment.item;

import java.util.ArrayList;
import java.util.Calendar;

import kr.hkit.password.R;
import kr.hkit.password.dao.ChangePassword;
import kr.hkit.password.dao.InfoDao;
import kr.hkit.password.db.MyDatabaseManager;
import android.app.Fragment;
import android.content.ContentValues;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

public class ItemAdd extends Fragment {
	private static final String TAG = "PW";
	private ArrayList<String> list;
	private EditText tvId;
	private EditText tvPw;
	private EditText tvUrl;
	private EditText tvName;
	private TextView tvCheck;
	private MyDatabaseManager mIdb;
	private Button btn;
	private Button btnRC;
	private Fragment fragment;
	private Bundle args;
	private InfoDao info;
	private Spinner spin;
	private ArrayAdapter<CharSequence> adspin;
	private ChangePassword cp;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		Log.d(TAG, "ItemAdd onCreateView");

		View view = inflater.inflate(R.layout.item_add, container, false);

		info = (InfoDao) getActivity().getApplication();
		info.setMain(false);
		tvCheck = (TextView) view.findViewById(R.id.tvCheck);
		tvId = (EditText) view.findViewById(R.id.id);
		tvPw = (EditText) view.findViewById(R.id.password);
		tvPw.addTextChangedListener(new TextWatcher() {
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				cp = new ChangePassword(tvPw.getText().toString());
				tvCheck.setText(cp.getCheck());
				if (cp.getCheck() == "위험") {
					tvCheck.setTextColor(Color.RED);
				} else if (cp.getCheck() == "보통") {
					tvCheck.setTextColor(Color.YELLOW);
				} else {
					tvCheck.setTextColor(Color.GREEN);
				}
			}
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,	int after) {}
			@Override
			public void afterTextChanged(Editable s) {}
		});
		tvUrl = (EditText) view.findViewById(R.id.url);
		tvName = (EditText) view.findViewById(R.id.name);
		spin = (Spinner) view.findViewById(R.id.alarmSpinner);
		spin.setPrompt("주기를 선택하세요.");
		adspin = ArrayAdapter.createFromResource(getActivity(), R.array.alarm,
				android.R.layout.simple_spinner_item);
		adspin.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spin.setAdapter(adspin);

		if (info.getUrl() != null) {
			tvUrl.setText(info.getUrl());
		}
		btn = (Button) view.findViewById(R.id.btn);
		btn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Log.d(TAG, "ItemAdd onClick");
				list = new ArrayList<String>();
				list.add(tvId.getText().toString());
				list.add(tvPw.getText().toString());
				list.add(tvUrl.getText().toString());
				list.add(tvName.getText().toString());
				list.add(spin.getSelectedItemPosition() + "");
				list.add(info.getAccount());
				mIdb = MyDatabaseManager.getInstance(getActivity());
				ContentValues addRowValue = new ContentValues();
				addRowValue.put("id", list.get(0));
				addRowValue.put("password", list.get(1));
				addRowValue.put("url", list.get(2));
				addRowValue.put("name", list.get(3));
				addRowValue.put("cycle", list.get(4));
				addRowValue.put("date",
						info.getDate(Calendar.getInstance().getTime()));
				addRowValue.put("account", list.get(5));
				mIdb.insert("list", addRowValue);
				fragment = new ItemView();
				args = new Bundle();
				args.putString(ItemView.ITEM_NAME, list.get(3));
				fragment.setArguments(args);
				getFragmentManager().beginTransaction()
						.replace(R.id.content_frame, fragment).commit();
			}
		});
		
		btnRC = (Button) view.findViewById(R.id.btnRC);
		btnRC.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				cp = new ChangePassword();
				tvPw.setText(cp.getRandomPw());
			}
		});

		return view;
	}
}
